package demo.selenium;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeDriver;
import java.sql.*;
import java.util.Date;
import java.util.Scanner;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/11/2 上午10:52
 * @UpdateDate:
 * @Description:
 */
public class LoginCmWeb {


    public static void main(String[] args) {
        // 浏览器驱动.
        System.setProperty("webdriver.chrome.driver", "/Users/icasue/Desktop/lbs-server-plugins/icasue-plugins-demos/icasue-demo-selenium/src/main/resources/drivers/chromedriver_98");
        // 创建浏览器驱动
        ChromeDriver driver = null;
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("代理环境::");
            String proxy_env = scanner.next().toUpperCase();
            System.out.println("代理用户::");
            String proxy_user = scanner.next();
            String token = null;

            // 访问环境.
            ENV env = ENV.valueOf(proxy_env);
            // token获取.
            token = touch_token(env, proxy_user);

            // 打开网站主页[redirect to /login].
            driver = new ChromeDriver();
            driver.get(env.web_url);

            // 添加token.
            int millionsSecondsInOneDay = 24 * 60 * 60 * 1000;
            Cookie cookie = new Cookie("token",token,"/",new Date(System.currentTimeMillis() + millionsSecondsInOneDay));
            driver.manage().addCookie(cookie);

            // 访问home页面.
            driver.get(env.web_url + "home");

            // 阻塞进程
            System.out.print("ACTION ENDING? :: ");
            scanner.next();
        }catch (Throwable e){
            e.printStackTrace();
        }finally {
            if(driver != null){
                // 退出驱动，退出驱动进程，远程驱动进程,
                // 关闭当前驱动打开的所有tab或者window， 关闭浏览器，清理session.
                driver.quit();
            }
        }
    }


    private static String touch_token(ENV env, String proxyUser){
        String token = null;
        String SQL = "select `token` from `cm_user` where `name` = '" + proxyUser + "'";
        if(env == ENV.PRE || env == ENV.PRD){
            token = ExecuteArchery.touch_token_from_archery(
                    ExecuteArchery.ENV.valueOf(env.name()),
                    "lbs_cm_microservice",
                    SQL
            );
        }else {
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                connection = DriverManager.getConnection(env.mysql_url, env.mysql_username, env.mysql_password);
                connection.setAutoCommit(true);
                statement = connection.createStatement();
                resultSet = statement.executeQuery("select `token` from `cm_user` where `name` = '" + proxyUser + "'");
                if(resultSet.next()){
                    token = resultSet.getString(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(resultSet != null){
                        resultSet.close();
                    }
                    if(statement != null){
                        statement.close();
                    }
                    if(connection != null){
                        connection.close();
                    }
                }catch (Throwable e2){
                    e2.printStackTrace();
                }
            }
        }
        return token;
    }


    enum ENV{
        DEV(
                "admin",
                "https://bt-lbs-config-dev.beantechyun.cn/",
                "jdbc:mysql://10.21.1.7:3306/lbs_cm_microservice?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                "nxengine",
                "XDhsj123"
        ),
        TEST(
                "admin",
                "https://bt-lbs-config-test.beantechyun.cn/",
                "jdbc:mysql://10.13.4.16:3306/lbs_cm_microservice?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                "nxengine",
                "XDmysql123"
        ),
        SIT(
                "admin",
                "https://bt-lbs-config-sit.beantechyun.cn/",
                "jdbc:mysql://tsp-sit-mysql.beantechyun.cn:3306/lbs_cm_microservice?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                "nxengine",
                "XDmysql123"
        ),
        PRE(
                "admin",
                "https://bt-lbs-config-pre.beantechyun.cn/",
                "",
                "",
                ""
        ),
        PRD("admin",
                "https://bt-lbs-config.beantechyun.com/",
                "",
                "",
                ""
        );
        public final String user;
        public final String web_url;
        public final String mysql_url;
        public final String mysql_username;
        public final String mysql_password;
        ENV(String user,String web_url,String mysql_url,String mysql_username,String mysql_password){
            this.user = user;
            this.web_url = web_url;
            this.mysql_url = mysql_url;
            this.mysql_username = mysql_username;
            this.mysql_password = mysql_password;
        }
    }
}
